package com.example.dispatherview;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;

public class MainActivity extends AppCompatActivity {


    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.iv);
        // getImageFormat();
        convertImage();
    }

    private void getImageFormat() {
        long time = System.currentTimeMillis();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), R.drawable.aaa, options);
        String outMimeType = options.outMimeType;
        String[] split = outMimeType.split("/");

    }

    private void convertImage() {
        long time = System.currentTimeMillis();
        long timer = System.currentTimeMillis();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.eeee);
        Log.d("convertImage", "解码图像: "+(System.currentTimeMillis() - time));
        time = System.currentTimeMillis();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 20, outputStream)) {
            Log.d("convertImage", "压缩图像: "+(System.currentTimeMillis() - time));

            BitmapFactory.Options options = new BitmapFactory.Options();

            options.inJustDecodeBounds = false;
            Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight());
            Log.d("convertImage", "压缩图像: "+(System.currentTimeMillis() - time));
            imageView.setImageBitmap(bitmap2);
            Log.d("convertImage", "time:" + (System.currentTimeMillis() - timer) + "\noutMimeType:" + options.outMimeType + "\nbounds:" + options.outWidth + "," + options.outHeight);
        }
    }
}
